#ifndef COOLPOOL_GLTOOLS_H_
#define COOLPOOL_GLTOOLS_H_

#include <Vector3D.h>

// FreeGlut hack:
// Use this enums for glutMouseFunc also for scrolling, because the
// glutMouseWheelFunc does not work.
enum
{
	MOUSE_LEFT_BUTTON = 0,
	MOUSE_MIDDLE_BUTTON = 1,
	MOUSE_RIGHT_BUTTON = 2,
	MOUSE_SCROLL_UP = 3,
	MOUSE_SCROLL_DOWN = 4
};

// Computes the 3D coordinates of the scene under the point on the screen.
inline math_tools::Vector3D GetOGLPos(int x, int y)
{
	GLint viewport[4];
	GLdouble modelview[16];
	GLdouble projection[16];
	GLfloat winX, winY, winZ;
	GLdouble posX, posY, posZ;
	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
	glGetDoublev(GL_PROJECTION_MATRIX, projection);
	glGetIntegerv(GL_VIEWPORT, viewport);
	winX = (float) x;
	winY = (float) viewport[3] - (float) y;
	glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
	gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
	return math_tools::Vector3D(posX, posY, posZ);
}

#endif
